package com.dehui.traders.ui.usercenter;

import android.content.Context;
import android.databinding.ObservableField;

import com.dehui.traders.bean.BaseResponse;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.ui.usercenter.EditTextActivity;
import com.dehui.traders.utils.UserHelp;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;
import retrofit2.Call;
import retrofit2.Response;

public class EditTextViewModel extends BaseViewModel{

    private EditTextActivity activity;

    public EditTextViewModel(Context context, EditTextActivity activity) {
        super(context);
        this.activity = activity;
    }

    public ObservableField<String> editMsg = new ObservableField<>();

    public BindingCommand onCompleteClick = new BindingCommand(()->{
        submit();
    });

    private void submit() {
        int type = activity.getmType();
        if (type == EditTextActivity.Type_Phone){
            saveUserPhone();
        }else if (type == EditTextActivity.Type_NickName){
            saveUserNickName();
        }else if (type == EditTextActivity.Type_Area){
            saveArea();
        }

    }

    private void saveArea() {
        Map<String,String> map = new HashMap<>();
        map.put("address",editMsg.get());
        Call<BaseResponse<Object>> call = NetImpl.saveUser(UserHelp.getUserId(), map);
        call.enqueue(new CallListener<BaseResponse<Object>>() {
            @Override
            public void _onResponse(Response<BaseResponse<Object>> response) {
                BaseResponse<Object> body = response.body();
                if (body.status == 200){
                    activity.finish();
                }else{
                    ToastUtils.showShort("设置失败");
                }
            }

            @Override
            public void _onFailure() {
                ToastUtils.showShort("网络异常");
            }
        });
    }

    private void saveUserNickName() {
        Map<String,String> map = new HashMap<>();
        map.put("userName",editMsg.get());
        Call<BaseResponse<Object>> call = NetImpl.saveUser(UserHelp.getUserId(), map);
        call.enqueue(new CallListener<BaseResponse<Object>>() {
            @Override
            public void _onResponse(Response<BaseResponse<Object>> response) {
                BaseResponse<Object> body = response.body();
                if (body.status == 200){
                    activity.finish();
                }else{
                    ToastUtils.showShort("设置失败");
                }
            }

            @Override
            public void _onFailure() {
                ToastUtils.showShort("网络异常");
            }
        });
    }

    private void saveUserPhone() {
        Map<String,String> map = new HashMap<>();
        map.put("phone",editMsg.get());
        Call<BaseResponse<Object>> call = NetImpl.saveUser(UserHelp.getUserId(), map);
        call.enqueue(new CallListener<BaseResponse<Object>>() {
            @Override
            public void _onResponse(Response<BaseResponse<Object>> response) {
                BaseResponse<Object> body = response.body();
                if (body.status == 200){
                    activity.finish();
                }else{
                    ToastUtils.showShort("设置失败");
                }
            }

            @Override
            public void _onFailure() {
                ToastUtils.showShort("网络异常");
            }
        });
    }


    public void setEditextText(String content) {
        editMsg.set(content);
    }
}
